<template>
  <div class="content-bg">
    <el-form :model="ruleForm" :rules="rules"
                ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idcard">
          <el-input v-model="ruleForm.idcard"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">返回</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>
<script lang="js">
import axios from "axios";

export default {
  name: 'myobstruct',
  data(){
    return {
        ruleForm: {
          name:'',
          idcard: '',
          phone: '',
        },
        rules: {
          name: [
            { required: true, message: '请输入id', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          idcard: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
          phone: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ]
        }
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    getAll: function (){
      axios.get("/userlist").then((res)=>{
        this.ruleForm = res.data.data
      })
    },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.put("/userlist",this.ruleForm).then((res)=>{
              if (res.data.code ===1){
                this.$message.success(res.data.data)
              }else {
                this.$message.error(res.data.msg)
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>
